package cn.yitun.chat.yitunserverimcore.general.main.biz.group.manager;

import cn.yitun.chat.yitunservercommonlib.converter.dto2vo.group.GroupCategoryDto2VoConverter;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupCategoryDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupCategoryVo;
import cn.yitun.chat.yitunservercommonlib.enums.groupChat.GroupCategoryDefaultEnums;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupCategoryDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: yitun
 * @since 1.1
 */
@Service
public class GroupCategoryManager {

	@Resource
	private GroupCategoryDao groupCategoryDAO;

	/**
	 * Date 2019-01-20 20:58:43<br>
	 * Description 获取默认的群分组，没有则创建
	 * @author yitun<br>
	 * @param userId
	 * @return
	 * @since 1.1
	 */
	public GroupCategoryVo getOrCreateDefault(Long userId) {
		GroupCategoryVo bean = groupCategoryDAO.getDefaultGroupCategory(userId);
		if (null == bean) {
			bean = new GroupCategoryVo();
			bean.setUserId(userId);
			bean.setName("我的聊天群");
			bean.setRank(0);
			bean.setSort(GroupCategoryDefaultEnums.SORT_DEFAULT.getType());
			bean.setCreateBy(userId);
			bean.setUpdateBy(userId);
			GroupCategoryDto dto = GroupCategoryDto2VoConverter.INSTANCE.vo2dto(bean);
			groupCategoryDAO.save(dto);
			bean.setId(dto.getId());
		}
		return bean;
	}

	/**
	 * Date 2019-01-24 22:38:28<br>
	 * Description
	 * @author yitun<br>
	 * @param userId
	 * @return
	 * @since 1.1
	 */
	public Long getOrCreateDefaultCategoryId(Long userId) {
		GroupCategoryVo bean = getOrCreateDefault(userId);
		return null != bean ? bean.getId() : null;
	}
}
